package com.workManager.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.workManager.mapper.TdRoleFunctionMapper;
import com.workManager.mapper.TdRoleMapper;
import com.workManager.pojo.*;
import com.workManager.vo.RoleFunctionVo;
import com.workManager.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Thinkpad on 2017/3/3 0003.
 */
@Service
public class RoleServiceImpl implements RoleService {

    public final static Map<String, List<String>> roleFunctionMap = new ConcurrentHashMap<String, List<String>>();



    @Autowired
    private TdRoleMapper roleMapper;
    @Autowired
    private TdRoleFunctionMapper roleFunctionMapper;
    public int add(TdRole role) {
        return roleMapper.insert(role);
    }

    public int update(TdRole role) {
        return roleMapper.updateByPrimaryKey(role);
    }

    public PageInfo<RoleVo> query(Map map) {
        int pageno = !map.containsKey("pagenum")||map.get("pagenum")==null?1:Integer.parseInt(map.get("pagenum").toString());
        int pagesize = !map.containsKey("pagesize")||map.get("pagesize")==null?10:Integer.parseInt(map.get("pagesize").toString());
        PageHelper.startPage(pageno,pagesize);
        List<RoleVo> roleList = roleMapper.query(map);
        PageInfo<RoleVo> rolePageInfo = new PageInfo<RoleVo>(roleList);
        return  rolePageInfo;

    }

    public int delete(String roleId) {
        int i = 0;
        Integer intRoleId = Integer.parseInt(roleId);
        TdRole role=roleMapper.selectByPrimaryKey(intRoleId);

        if (null!=role){
            //删除本角色
            roleMapper.deleteByPrimaryKey(intRoleId);
            i++;
            //删除子角色
            TdRoleExample example=new TdRoleExample();
            example.createCriteria().andParentRoleIdEqualTo(intRoleId);
            List<TdRole> childRoleList=roleMapper.selectByExample(example);

            if (childRoleList!=null){
                for (TdRole childRole:childRoleList){
                    if (childRole!=null&&childRole.getRoleId()!=null){
                        //递归调用
                        i+= delete(String.valueOf(childRole.getRoleId()));
                    }
                }
            }
        }
        return i;
    }

    public int deleteByList(List<String> roleIds) {
        int i = 0;
        for(String roleId:roleIds){
            i += delete(roleId);
        }
        return i;
    }

    public int deleteRoleFunctionByRoleId(Integer roleId) {
        return roleFunctionMapper.deleteByRoleId(roleId);
    }

    public int insertRoleFunction(TdRoleFunction roleFunction) {
        return roleFunctionMapper.insert(roleFunction);
    }

    public int insertRoleFunctionBatch(List<Map<String,Object>> list) {
         roleFunctionMapper.insertRoleFunctionBatch(list);
        initRoleFunctionMap();
        return  1;
    }

    public PageInfo<RoleFunctionVo> queryRoleFunction(Map map) {
        int pageno = !map.containsKey("pagenum")||map.get("pagenum")==null?1:Integer.parseInt(map.get("pagenum").toString());
        int pagesize = !map.containsKey("pagesize")||map.get("pagesize")==null?10:Integer.parseInt(map.get("pagesize").toString());
        PageHelper.startPage(pageno,pagesize);
        List<RoleFunctionVo> roleFunctionVos = roleFunctionMapper.queryRoleFunction(map);
        PageInfo<RoleFunctionVo> roleFunctionVoPageInfo = new PageInfo<RoleFunctionVo>(roleFunctionVos);
        return roleFunctionVoPageInfo;
    }

    public List<String> queryRoleFunctionId(String roleId) {
        return roleFunctionMapper.queryRoleFunctionId(roleId);
    }

    public void  initRoleFunctionMap(){
        List<TdRole> roleList =  roleMapper.selectByExample(null);
        for(int i =0;i<roleList.size();i++){
           List<String>  functionCodeList =  roleFunctionMapper.queryRoleFunctionCode(roleList.get(i).getRoleId().toString());
            roleFunctionMap.put(roleList.get(i).getRoleId().toString(),functionCodeList);
        }
    }

    public boolean checkRoleFunction(String roleId,String functionCode){
        List<String> functionCodeList = roleFunctionMap.get(roleId);
        for(int i=0;i<functionCodeList.size();i++){
            if(functionCode.contains(functionCodeList.get(i))){
                return true;
            }
        }
        return false;
    }


    public List<String> getRoleFunctionCodeByUserId(Integer userId){
        return roleFunctionMapper.getRoleFunctionCodeByUserId(userId);
    }
}
